<?php
namespace common\components\services\goods;
use common\components\helpers\Fun;
use EasyWeChat\Server\BadRequestException;
use frontend\models\datas\ClassManage;
use frontend\models\datas\Goods;
use frontend\models\datas\GoodsAttr;
use yii\base\Action;
use yii\db\Exception;
use yii\helpers\ArrayHelper;
use yii\web\NotFoundHttpException;


class ShelveAction extends Action{
    public $type;
    public function run(){
        if(\Yii::$app->request->isAjax){
            if(\Yii::$app->request->post('id') == null){
                throw new BadRequestException(\Yii::t('frontend','商品id为空'));
            }
            $id=\Yii::$app->request->post('id');
            $goods = Goods::findOne($id);
            if($goods==null){
                throw new NotFoundHttpException(\Yii::t('frontend','信息不存在'));
            }
            $status = 10;
            if($goods->is_shelve == 2){
                $status = 11;
                return $status;
            }
            //上架
            if($goods->is_shelve==0 && ($goods->deposit+$goods->admission_fee+$goods->cost)>0){
                    $goods->is_shelve=1;
                    $goods->up_at=time();
                    if($goods->save(false)){
                        $classmanage=ClassManage::findOne(['is_old'=>0,'associate_id'=>$goods->id]);
                        if($classmanage==null){
                            $goods_attr=GoodsAttr::findOne(['goods_id'=>$goods->id,'obj_type'=>'is_booking']);
                            $class_manage=new ClassManage();
                            $class_manage->class_code='c'.time();
                            $class_manage->is_book=$goods_attr!=null?(int)($goods_attr->value):null;
                            $class_manage->is_old=0;
                            $class_manage->associate_id=$goods->id;
                            $class_manage->mechanic_id=$goods->mechanic_id;
                            $class_manage->save(false);
                        }else{
                           $classmanage->is_delete = 0;
                           $classmanage->save(false);
                        }
                    }
                    $status = 1;
            }
            //申请中
            elseif($goods->is_shelve==0 && ($goods->deposit+$goods->admission_fee+$goods->cost) <=0){
                $goods->is_shelve=4;
                $goods->up_at=time();
                $goods->save(false);
                $status = 4;
            }
            //取消申请
            elseif($goods->is_shelve==4 && ($goods->deposit+$goods->admission_fee+$goods->cost)<=0) {
                $goods->is_shelve=0;
                $goods->up_at=time();
                $goods->save(false);
                $status = 3;
            }
            //下架
            elseif($goods->is_shelve==1){
                $goods->is_shelve=0;
                $goods->down_at=time();
                $goods->save(false);
                $status = 0;
            }
           return $status;
        }
    }

//
//    public function check_can_shelve($goods_id){
//        $info_id=GoodsAttr::find()->where(['goods_id'=>$goods_id,'obj_type'=>'start_time'])->one(Fun::getSiteDb());
//        $goods_start_time=GoodsAttr::find()->select('goods_id')->where(['attribute_id'=>$info_id->attribute_id,'obj_type'=>'start_time','value'=>$info_id->value])->column(Fun::getSiteDb());
//        if(empty($goods_start_time)){
//            return true;
//        }else{
//            $info=GoodsAttr::find()->where(['goods_id'=>$goods_id])->asArray()->all(Fun::getSiteDb());
//            $info_array=ArrayHelper::map($info,'obj_type','value');
////            var_dump($info_array);die;
//            $goods_end_time=GoodsAttr::find()->select('goods_id')->where(['in','goods_id',$goods_start_time])->andWhere(['attribute_id'=>$info_id->attribute_id,'obj_type'=>'end_time','value'=>$info_array['end_time']])->column(Fun::getSiteDb());
//            if(empty($goods_end_time)){
//                return true;
//            }else{
//                $goods_class_day=GoodsAttr::find()->select('goods_id')->where(['in','goods_id',$goods_end_time])->andWhere(['attribute_id'=>$info_id->attribute_id,'obj_type'=>'class_day','value'=>$info_array['class_day']])->column(Fun::getSiteDb());
//                if(empty($goods_class_day)){
//                    return true;
//                }else{
//                    $goods=Goods::find()->where(['in','id',$goods_class_day])->andWhere(['is_delete'=>0,'is_shelve'=>1])->one(Fun::getSiteDb());
//                    if($goods==null){
//                        return true;
//                    }else{
//                        return false;
//                    }
//                }
//            }
//        }
//    }
}
?>
